<!-- 用户的文章 -->
  <template>
    <div>
      <a-select
        show-search
        placeholder="选择分类"
        option-filter-prop="children"
        style="width: 200px"
        :filter-option="filterOption"
        @change="handleChange"
        v-if="isAdmin"
      >
        <a-select-option value="前端">
          前端技术
        </a-select-option>
        <a-select-option value="后端">
          后端技术
        </a-select-option>
        <a-select-option value="疑问0">
          已解决的问题
        </a-select-option>
        <a-select-option value="疑问1">
          未解决的问题
        </a-select-option>
        <a-select-option value="其他技术">
          其他技术
        </a-select-option>
      </a-select>


      <a-list
        item-layout="horizontal"
        :data-source="listData"
        :pagination="pagination"
      >
        <a-list-item slot="renderItem" slot-scope="item">
          <a slot="actions" v-if="isAdmin" @click="recycleOrReBackArticle(item.id, item.status)">
            {{item.status ? '回收':'恢复'}}
          </a>
          <a slot="actions" v-if="isAdmin" @click="deleteOpera(item.id)">删除</a>

          <a-list-item-meta :description="'发布时间：' + item.createTime">
            <a slot="title" @click="getDetails(item.id)">{{ item.title }}</a>
          </a-list-item-meta>
        </a-list-item>
      </a-list>
    </div>
  </template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getUserArticle, getAdminArticleList } from "@/apis/article";
import { recycleOrReBack, deleteArticle } from "@/apis/admin";
export default {
  // import引入的组件需要注入到对象中才能使用
  components: {},
  props: {
    currentKey: String
  },
  data() {
    // 这里存放数据
    return {
      listData: [],
      loading: false,
      pagination: {
        current: 1,
        limit: 5,
        total: 0,
        pageSize: 5,
        hideOnSinglePage: true,
        onChange: (page) => {
          let data = {
            page: page,
            limit: this.pagination.limit,
          };
          this.loading = true;
          if(this.isAdmin) {
            data.type = this.type;
            getAdminArticleList(data).then((res) => {
              this.listData = res.data.list
              this.pagination.current = page;
              this.pagination.total = res.data.total;
              this.loading = false;
              $(window).scrollTop(0);
            });
          } else {
            data.userId = this.$route.params.id;
            getUserArticle(data).then((res) => {
              this.listData = res.data.list
              this.pagination.current = page;
              this.pagination.total = res.data.total;
              this.loading = false;
              $(window).scrollTop(0);
            });
          }

        },
      },
      isAdmin: false,
      type: ""
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    currentKey(nv, ov) {
      if (nv === '7') {
        this.isAdmin = true;
        this.getAdminPageArticle('');
      } else {
        this.isAdmin = false;
        this.getRouterPageArticle();
      }
    }
  },
  //方法集合
  methods: {
    getRouterPageArticle() {
      let data = {
        page: 1,
        limit: 5,
        userId: this.$route.params.id,
      };
      getUserArticle(data).then((res) => {
        this.listData = res.data.list;
        this.pagination.total = res.data.total;
      });
    },
    getAdminPageArticle(type) {
      let data = {
        page: 1,
        limit: 5,
        type: type,
      };
      getAdminArticleList(data).then((res) => {
        this.listData = res.data.list;
        this.pagination.total = res.data.total;
      })
    },
    handleChange(value) {
      this.type = value;
      this.getAdminPageArticle(value);
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      );
    },
    recycleOrReBackArticle(id ,status) {
      let data = {
        id: id,
        status: !status
      }
      recycleOrReBack(data).then(res => {
        this.getAdminPageArticle(this.type);
      })
    },
    getDetails(id) {
      this.$router.push("/Details/"+id);
    },
    deleteOpera(id) {
      let data = {
        id: id
      }
      deleteArticle(data).then(res => {
        this.getAdminPageArticle(this.type);
      })
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    if (!this.isAdmin) {
      this.getRouterPageArticle();
    } else {
      this.getAdminPageArticle('');
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类
</style>
